package com.zhp.farmerclient.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhp.farmerclient.common.Result;
import com.zhp.farmerclient.entity.Announce;
import com.zhp.farmerclient.entity.Post;
import com.zhp.farmerclient.mapper.AnnounceMapper;
import com.zhp.farmerclient.mapper.PostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchService {


    @Autowired
    public AnnounceMapper announceMapper;
    @Autowired
    public PostMapper postMapper;



    //搜索结果分页显示
    public Result search(Integer page, Integer pageSize,String keyword, String type) {
        // 执行搜索操作，返回原始搜索结果列表
        if (type.equals("1")) {
            System.out.println("公告搜索结果");
            Page<Announce> announcePage = new Page<>(page,pageSize);
            QueryWrapper<Announce> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("title",keyword).or().like("content",keyword).or().like("time",keyword);
            queryWrapper.orderByDesc("id");
            IPage<Announce> iPage = announceMapper.selectPage(announcePage,queryWrapper);
            return Result.succ(iPage.getRecords(), iPage.getTotal());
        }else if(type.equals("2")){
            System.out.println("焦点讨论搜索结果");
            Page<Post> postPage = new Page<>(page,pageSize);
            QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("title",keyword).or().like("content",keyword).or().like("time",keyword);;
            queryWrapper.orderByDesc("id");
            IPage<Post> iPage = postMapper.selectPage(postPage,queryWrapper);
            return Result.succ(iPage.getRecords(), iPage.getTotal());
        }

        return Result.succ();
    }
}
